﻿using System;
using System.Runtime.Serialization;

namespace Eflong.DTO.Wcf
{
    [DataContract]
    public class CoalMineInfo
    {
        public Int32 AutoId;

        /// <summary>
        /// 矿Id
        /// </summary>
        public Int32 Id;

        /// <summary>
        /// 矿名
        /// </summary>
        public string Name;

        /// <summary>
        ///  矿IP地址
        /// </summary>
        public string IPAddr;

        /// <summary>
        /// 联网状态 0 未使用/ 1 正常/2 报警/ 3 故障
        /// </summary>
        public ConnectStates ConnectionStatus = ConnectStates.UnKnownState;

        /// <summary>
        /// 联网时刻
        /// </summary>
        public DateTime Time;

        /// <summary>
        /// 数据变化时刻
        /// </summary>
        public DateTime ChangedTime;

        /// <summary>
        /// 中断开始时刻
        /// </summary>
        public DateTime BeginTime = DateTime.Now;

        /// <summary>
        /// 中断结束时刻
        /// </summary>
        public DateTime EndTime;

        /// <summary>
        /// 异常原因
        /// </summary>
        public string Causation;

        /// <summary>
        /// 采取措施
        /// </summary>
        public string Measure;

        /// <summary>
        /// 采取措施时刻
        /// </summary>
        public DateTime MeasureTime;

        /// <summary>
        /// 已经记录
        /// </summary>
        public bool IsRecord;

        /// <summary>
        /// 已经记录到历史表
        /// </summary>
        public bool IsHisRecord;

        /// <summary>
        /// 需要记录
        /// </summary>
        public bool IsNeedRecord;

        /// <summary>
        /// 全部测点数量
        /// </summary>
        public int AllSensorCount;

        /// <summary>
        /// 正常通信测点数量
        /// </summary>
        public int NormalSensorCount;

        /// <summary>
        /// 其中报警测点数据
        /// </summary>
        public int AlarmSensorCount;

        /// <summary>
        /// 故障测点数据
        /// </summary>
        public int FaultSensorCount;

        //
        /// <summary>
        /// 状态字符串
        /// </summary>
        public string StateString;

        public CoalMineInfo( )
        {
            Id = 0;
            Name = string.Empty;
            IPAddr = "0.0.0.0";
            ConnectionStatus = ConnectStates.UnKnownState;
            TimeSpan ts = new TimeSpan( 0 , 5 , 0 );
            Time = DateTime.Now.Subtract( ts );
            ChangedTime = Time;
            BeginTime = DateTime.Now;
            IsRecord = false;
            IsNeedRecord = false;
            IsHisRecord = false;
            AllSensorCount = 0;
            NormalSensorCount = 0;
            FaultSensorCount = 0;
            AlarmSensorCount = 0;
        }

        /// <summary>
        /// 获取联网状态字符串
        /// </summary>
        /// <returns></returns>
        public String GetConnectStateString( )
        {
            switch ( ConnectionStatus )
            {
                case ConnectStates.FaultState:
                    StateString = "网络中断";
                    break;

                case ConnectStates.NoChangedState:
                    StateString = "数据不变化";
                    break;

                case ConnectStates.UnKnownState:
                    StateString = "未知";
                    break;

                case ConnectStates.NormalState:
                    StateString = "正常";
                    break;

                case ConnectStates.AlertState:
                    StateString = "报警";
                    break;
                default:
                    StateString = "未知";
                    break;
            }
            return StateString;
        }

        /// <summary>
        /// 是否正常
        /// </summary>
        /// <returns></returns>
        public bool IsNormal( )
        {
            if ( ConnectionStatus >= ConnectStates.NormalState && ConnectionStatus <= ConnectStates.AlertState )
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 是否中断
        /// </summary>
        /// <returns></returns>
        public bool IsFault( )
        {
            if ( ConnectionStatus == ConnectStates.UnKnownState || ConnectionStatus >= ConnectStates.FaultState || ConnectionStatus >= ConnectStates.NoChangedState )
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 是否需要记录

        /// </summary>
        /// <returns></returns>
        public bool IsNeedToRecord( )
        {
            double time = DateTime.Now.Subtract( BeginTime ).TotalSeconds;
            if ( time >= ChangedTimeDelay )
            {
                // 故障超过 60 秒,认为是 真故障
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 设置需要记录
        /// </summary>
        public void SetToNeedRecord( )
        {
            IsRecord = false;
            IsNeedRecord = true;
        }

        /// <summary>
        /// 设置已经记录
        /// </summary>
        public void SetHasRecord( )
        {
            IsRecord = true;
            IsNeedRecord = false;
        }

        /// <summary>
        /// 改变到正常状态
        /// </summary>
        public void SetNormal( )
        {
            if ( IsFault( ) )
            {
                ConnectionStatus = ConnectStates.NormalState;
                EndTime = ChangedTime;
                SetToNeedRecord( );
            }
            else if ( !IsRecord )
            {
                // 两次正常，则认为正常；
                //IsNeedRecord = IsNeedToRecord();
                if ( DateTime.Now.Subtract( EndTime ).TotalSeconds > 30 )
                {
                    IsNeedRecord = true;
                }
            }
        }

        /// <summary>
        /// 改变到异常状态
        /// </summary>
        public void SetFault( )
        {
            if ( ConnectionStatus != ConnectStates.FaultState )
            {
                ConnectionStatus = ConnectStates.FaultState;
                BeginTime = ChangedTime;
                SetToNeedRecord( );
            }
            else if ( !IsRecord )
            {
                // 故障时间超过 60 秒
                IsNeedRecord = IsNeedToRecord( );
            }
        }

        public const int ChangedTimeDelay = 60 * 5;

        /// <summary>
        /// 是否数据不变化
        /// </summary>
        /// <returns></returns>
        public bool IsDataNoChanged( )
        {
            // 联网状态正常，而数据长时间不变化，变化时刻超过指定的延迟时间,认为,未更新
            if ( IsNormal( ) && DateTime.Now.Subtract( ChangedTime ).TotalSeconds > ChangedTimeDelay )
            {
                // 不变化时间超过 60 秒,认为是数据不变化了
                return true;
            }
            return false;
        }

        /// <summary>
        /// 改变到数据不变化状态
        /// </summary>
        public void SetDataNoChanged( )
        {
            if ( ConnectionStatus != ConnectStates.NoChangedState )
            {
                ChangedTime = DateTime.Now;
                ConnectionStatus = ConnectStates.NoChangedState;
                BeginTime = ChangedTime;
                SetToNeedRecord( );
            }
            else if ( !IsRecord )
            {
                // 不变化时间超过 60 秒,认为是数据不变化了

                IsNeedRecord = IsNeedToRecord( );
            }
        }
    }
}